/*
 * md2.h -- md2 model header file
 *
 * Copyright (c) 2005-2007 David HENRY
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef _MD2_H_
#define _MD2_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "../../math/src/mgpi_math.h"

#pragma pack(push,1)

    /* Vector */
    typedef float mvec3_t[3];

    /* MD2 header */
    struct _md2_header {
        int ident;
        int version;

        int skinwidth;
        int skinheight;

        int framesize;

        int num_skins;
        int num_vertices;
        int num_st;
        int num_tris;
        int num_glcmds;
        int num_frames;

        int offset_skins;
        int offset_st;
        int offset_tris;
        int offset_frames;
        int offset_glcmds;
        int offset_end;
    };

    /* Texture name */
    struct _md2_skin {
        char name[64];
    };

    /* Texture coords */
    struct _md2_texCoord {
        short s;
        short t;
    };

    /* Triangle info */
    struct _md2_triangle {
        unsigned short vertex[3];
        unsigned short st[3];
    };

    /* Compressed vertex */
    struct _md2_vertex {
        unsigned char v[3];
        unsigned char normalIndex;
    };

    /* Model frame */
    struct _md2_frame {
        mvec3_t scale;
        mvec3_t translate;
        char name[16];
        struct _md2_vertex *verts;
    };

    /* GL command packet */
    struct _md2_glcmd {
        float s;
        float t;
        int index;
    };

    /* MD2 model structure */
    typedef struct _md2_model {
        struct _md2_header header;

        struct _md2_skin *skins;
        struct _md2_texCoord *texcoords;
        struct _md2_triangle *triangles;
        struct _md2_frame *frames;
        int *glcmds;

        GLuint *tex_ids;

        void (*vertex_attribute)(const struct _md2_model *);
    } md2_model_t;

#pragma pack(pop)

    int MGPI_API gsubReadMD2Model(const char *, md2_model_t *);
    void MGPI_API gsubFreeMD2Model(md2_model_t *);
    void MGPI_API gsubRenderMD2Frame(int, const md2_model_t *);
    void MGPI_API gsubRenderMD2FrameItp(int, float, const md2_model_t *);
    void MGPI_API gsubRenderMD2FrameWithGLCmds(int, const md2_model_t *);
    void MGPI_API gsubRenderMD2FrameItpWithGLCmds(int, float, const md2_model_t *);
    void MGPI_API gsubAnimateMD2(int, int, int *, float *);

#ifdef __cplusplus
}
#endif

#endif
